package com.infinit.easyreader.adpter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.infinit.easyreader.R;
import com.infinit.easyreader.entity.Bookmark;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 类：com.infinit.easyreader.adpter.BookMarkAdapter V[1.0.0]
 * 作用：书签List的展示页面
 * Created by rzy on 2016/5/25.
 */
public class BookmarkAdapter extends BaseAdapter<Bookmark> {
    private int bookLength;

    public BookmarkAdapter(List<Bookmark> dataList, Context context, int bookLength) {
        super(dataList, context);
        this.bookLength = bookLength;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.mark_content, null);
            viewHolder = new ViewHolder();
            viewHolder.tvMarkProgress = (TextView) convertView.findViewById(R.id.tvMarkName);
            viewHolder.tvMarkQuote = (TextView) convertView.findViewById(R.id.tvMarkContent);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Bookmark mark = getDataList().get(position);
        DecimalFormat df = new DecimalFormat("#0.0");
        float percent = (float) (mark.getPosition() * 1.0) / bookLength;
        String strPercent = df.format(percent * 100) + "%";
        viewHolder.tvMarkProgress.setText(strPercent);
        viewHolder.tvMarkQuote.setText(mark.getQuote().trim());
        return convertView;
    }

    private class ViewHolder {
        TextView tvMarkProgress;
        TextView tvMarkQuote;
    }


}
